package com.cyy.system.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cyy.common.domain.Response;
import com.cyy.common.pojo.dto.DictDataFormDto;
import com.cyy.common.pojo.dto.DictTypeFormDto;
import com.cyy.common.pojo.query.system.SystemDictDataQuery;
import com.cyy.common.pojo.query.system.SystemDictTypeQuery;
import com.cyy.common.pojo.vo.system.SystemDictDataVo;
import com.cyy.common.pojo.vo.system.SystemDictTypeVo;
import com.cyy.common.service.DictDataService;
import com.cyy.common.service.DictTypeService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Tag(name = "字典")
@RestController
@RequestMapping("/system/dict")
@RequiredArgsConstructor
@SaCheckLogin
public class DictController {

    private final DictDataService dictDataService;

    private final DictTypeService dictTypeService;


    @GetMapping("/types/page")
    public Response typesPage(SystemDictTypeQuery query) {
        IPage<SystemDictTypeVo> res = dictTypeService.getPage(query);
        return Response.success().data(res);
    }

    @GetMapping("/data/page")
    public Response dataPage(@Validated SystemDictDataQuery query) {
        IPage<SystemDictDataVo> res = dictDataService.getPage(query);
        return Response.success().data(res);
    }

    @GetMapping("/types/{id}/form")
    public Response getDictTypeForm(@PathVariable("id") Integer id) {
        DictTypeFormDto res = dictTypeService.getDictTypeFormById(id);
        return Response.success().data(res);
    }

    @PostMapping("/types")
    public Response addDictType(@RequestBody DictTypeFormDto req) {
        dictTypeService.addDictType(req);
        return Response.success();
    }

    @PutMapping("/types")
    public Response updateDictType(@RequestBody DictTypeFormDto req) {
        dictTypeService.updateDictType(req);
        return Response.success();
    }

    @DeleteMapping("/types/{ids}")
    public Response deleteDictTypes(@PathVariable("ids") @NotBlank String ids) {
        dictTypeService.deleteDictTypes(ids);
        return Response.success();
    }

    @GetMapping("/data/{typeCode}/options")
    public Response getDictOptions(@PathVariable("typeCode") String typeCode) {
        return Response.success().data(dictDataService.getDictOptions(typeCode));
    }

    @GetMapping("/data/{id}/form")
    public Response getDictFormData(@PathVariable("id") @NotNull Integer id) {
        DictDataFormDto res = dictDataService.getDictFormData(id);
        return Response.success().data(res);
    }

    @PostMapping("/data")
    public Response addDict(@RequestBody DictDataFormDto dto) {
        dictDataService.addDict(dto);
        return Response.success();
    }

    @PutMapping("/data")
    public Response updateDict(@RequestBody DictDataFormDto dto) {
        dictDataService.updateDict(dto);
        return Response.success();
    }

    @DeleteMapping("/data/{ids}")
    public Response deleteDict(@PathVariable("ids") String ids) {
        dictDataService.deleteDict(ids);
        return Response.success();
    }
}
